var file = require("../models/file.js");
var formidable = require('formidable');
var path = require("path");
var fs = require("fs");
var sd = require("silly-datetime");

exports.showIndex = (req, res, next) => {
    file.getAllAlbums((err, allAlabums) => {
        if (err) {
            next(); 
            return;
        }  
        res.render("index", {
            "albums": allAlabums
        });
    })
}


exports.showAlbum = (req, res, next) => {
    var albumName = req.params.albumName;
    file.getAllImagesByAlbumName(albumName, (err, imagesArray) => {
        if (err) {
            next(); 
            return;
        }
        res.render("album", {
            "albumname": albumName,
            "images": imagesArray
        });
    });
};


exports.showUp = (req, res) => {
    file.getAllAlbums((err, albums) => {
        res.render("up", {
            albums: albums
        });
    });
};


//上传图片
exports.doPost = (req, res) => {
    var form = new formidable.IncomingForm();
    form.uploadDir = path.normalize(__dirname + "/../tempup/");
    fs.readdir(form.uploadDir, (err) => {
        if (err) {
            fs.mkdir(form.uploadDir, (err) => {
                if (err) { throw err }
                form.parse(req, (err, fields, files, next) => {
                    
                    if (err) {
                        next();    
                        return;
                    }
                    
                    var size = parseInt(files.tupian.size);
                    if (size > 2000000000) {
                        res.send("图片尺寸应该小于10M");
                        fs.unlink(files.tupian.path);
                        return;
                    }
                    var ttt = sd.format(new Date(), 'YYYYMMDDHHmmss');
                    var ran = parseInt(Math.random() * 89999 + 10000);
                    var extname = path.extname(files.tupian.name);

                    var wenjianjia = fields.wenjianjia;
                    var oldpath = files.tupian.path;
                    var newpath = path.normalize(__dirname + "/../uploads/" + wenjianjia + "/" + ttt + ran + extname);
                    fs.rename(oldpath, newpath, (err) => {
                        if (err) {
                            res.send("上传失败");
                            return;
                        }
                        res.redirect(`/${wenjianjia}`)
                    });
                });
                return;
            })
        }
        form.parse(req, (err, fields, files, next) => {
            if (err) {
                next();     
                return;
            }
            var size = parseInt(files.tupian.size);
            if (size > 2000000000) {
                res.send("图片尺寸应该小于10M");
                fs.unlink(files.tupian.path);
                return;
            }
            var ttt = sd.format(new Date(), 'YYYYMMDDHHmmss');
            var ran = parseInt(Math.random() * 89999 + 10000);
            var extname = path.extname(files.tupian.name);

            var wenjianjia = fields.wenjianjia;
            var oldpath = files.tupian.path;
            var newpath = path.normalize(__dirname + "/../uploads/" + wenjianjia + "/" + ttt + ran + extname);
            fs.rename(oldpath, newpath, (err) => {
                if (err) {
                    res.send("上传失败");
                    return;
                }
                res.redirect(`/${wenjianjia}`)
            });
        });
        return;
    })
}


exports.createDir = (req, res) => {
    res.render("createdir")
}

exports.doPostdir = (req, res) => {
    file.createdir(req.body, (err, data) => {
        if (err) throw err
        res.send(data)
    })
}


exports.removedir = (req, res) => {
    file.removedir(req.body, (err) => {
        if (err) throw err
        res.send({ "code": 0 })
    })
}

exports.renamedir = (req, res) => {
    file.renamedir(req.body, (err, data) => {
        if (err) throw err
        res.send(data)
    })

}


exports.removefile = (req, res) => {
    file.removefile(req.body, (err, data) => {
        if (err) throw err
        res.send({ "code": 0 })
    })
}
